import java.util.Arrays;
class Solution {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public TreeNode sortedArrayToBST(int[] nums) {
        return sorted(nums,0,nums.length-1);
    }
    public TreeNode sorted(int[] nums, int left,int right){
        if(left > right){
            return null;
        }
        //总是选择中间位置左边的数字作为根节点
        int mid = (left+ right) / 2;

        TreeNode root = new TreeNode(nums[mid]);
        root.left = sorted(nums,left,mid-1);
        root.right = sorted(nums,mid+1,right);
        return root;
    }
}
public class Test {
    public static void main(String[] args) {
        int [] nums = {1,-2,-4,4,5,6};
        Solution s = new Solution();
        s.sortedArrayToBST(nums);
        System.out.println(Arrays.toString(nums));

    }
}
